//
//  ZSHomeGridViewController.m
//  ZhuShou
//
//  Created by Dejun Liu on 15/11/2.
//  Copyright © 2015年 jw. All rights reserved.
//

#import "ZSHomeGridViewController.h"
#import "ZSGMOneCell.h"
#import "GMGridView.h"

@interface ZSHomeGridViewController ()<GMGridViewDataSource, GMGridViewSortingDelegate, GMGridViewTransformationDelegate, GMGridViewActionDelegate>

@property (nonatomic,strong) GMGridView *gmGridView;
@property (nonatomic,strong) NSMutableArray *listData;

@end

@implementation ZSHomeGridViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.clipsToBounds = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initViews
{
    [super initViews];
    [self loadCollectionView];
}

- (void)loadCollectionView
{
    NSArray *array = @[@{@"title":@"客厅灯泡",@"img":@"menu_icon_camera"},
                       @{@"title":@"客厅灯泡",@"img":@"menu_icon_camera"},
                       @{@"title":@"客厅灯泡",@"img":@"menu_icon_camera"},
                       @{@"title":@"客厅灯泡",@"img":@"menu_icon_bulbs"},
                       @{@"title":@"客厅灯泡",@"img":@"menu_icon_bulbs"},
                       @{@"title":@"客厅灯泡",@"img":@"menu_icon_bulbs"},
                       @{@"title":@"客厅灯泡",@"img":@"menu_icon_bulbs"},
                       @{@"title":@"客厅灯泡",@"img":@"menu_icon_bulbs"},
                       @{@"title":@"客厅灯泡",@"img":@"menu_icon_bulbs"},
                       @{@"title":@"客厅灯泡",@"img":@"menu_icon_bulbs"},
                       @{@"title":@"客厅灯泡",@"img":@"menu_icon_bulbs"},
                       @{@"title":@"客厅灯泡",@"img":@"menu_icon_bulbs"},
                       @{@"title":@"客厅灯泡",@"img":@"menu_icon_bulbs"},
                       @{@"title":@"客厅灯泡",@"img":@"menu_icon_bulbs"},
                       @{@"title":@"客厅灯泡",@"img":@"menu_icon_bulbs"},
                       @{@"title":@"客厅灯泡",@"img":@"menu_icon_bulbs"},
                       @{@"title":@"客厅灯泡",@"img":@"menu_icon_bulbs"},
                       @{@"title":@"客厅灯泡",@"img":@"menu_icon_bulbs"},
                       @{@"title":@"客厅灯泡",@"img":@"menu_icon_bulbs"},
                       @{@"title":@"客厅灯泡",@"img":@"menu_icon_bulbs"},
                       @{@"title":@"客厅灯泡",@"img":@"menu_icon_bulbs"},
                       @{@"title":@"客厅灯泡",@"img":@"menu_icon_bulbs"},
                       @{@"title":@"客厅灯泡",@"img":@"menu_icon_bulbs"},
                       @{@"title":@"客厅灯泡",@"img":@"menu_icon_bulbs"},
                       @{@"title":@"客厅灯泡",@"img":@"menu_icon_bulbs"},
                       @{@"title":@"客厅灯泡",@"img":@"menu_icon_bulbs"},
                       @{@"title":@"客厅灯泡",@"img":@"menu_icon_bulbs"},
                       ];
    self.listData = [NSMutableArray arrayWithArray:array];
    
    NSInteger spacing = 15;
    
    GMGridView *gmGridView = [[GMGridView alloc] initWithFrame:self.view.bounds];
    gmGridView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    gmGridView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:gmGridView];
    _gmGridView = gmGridView;
    
    _gmGridView.style = GMGridViewStyleSwap;
    _gmGridView.itemSpacing = spacing;
    _gmGridView.minEdgeInsets = UIEdgeInsetsMake(spacing, spacing, spacing, spacing);
    _gmGridView.centerGrid = NO;
    _gmGridView.actionDelegate = self;
    _gmGridView.sortingDelegate = self;
    _gmGridView.transformDelegate = self;
    _gmGridView.dataSource = self;
    
    //    _gmGridView.enableEditOnLongPress = YES;
}

- (void)finishedEditing
{
    self.gmGridView.editing = NO;
}

- (void)rightBarItem:(BOOL)isShow
{
    if (isShow) {
        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(finishedEditing)];
        self.navigationItem.rightBarButtonItem = item;
    }else
    {
        self.navigationItem.rightBarButtonItem = nil;
    }
}

#pragma mark GMGridView

- (NSInteger)numberOfItemsInGMGridView:(GMGridView *)gridView
{
    return self.listData.count;
}

- (CGSize)GMGridView:(GMGridView *)gridView sizeForItemsInInterfaceOrientation:(UIInterfaceOrientation)orientation
{
    if (DEVICE_IS_PHONE)
    {
        if (UIInterfaceOrientationIsLandscape(orientation))
        {
            return CGSizeMake(170, 135);
        }
        else
        {
            return CGSizeMake(60, 60);
        }
    }
    else
    {
        if (UIInterfaceOrientationIsLandscape(orientation))
        {
            return CGSizeMake(285, 205);
        }
        else
        {
            return CGSizeMake(230, 175);
        }
    }
}

- (GMGridViewCell *)GMGridView:(GMGridView *)gridView cellForItemAtIndex:(NSInteger)index
{
    static NSString *identifier = @"Cell";
    
    ZSGMOneCell *cell = (ZSGMOneCell *)[gridView dequeueReusableCellWithIdentifier:identifier];
    
    if (!cell)
    {
        cell = [[ZSGMOneCell alloc] initWithReuseIdentifier:identifier];
        cell.deleteButtonIcon = [UIImage imageNamed:@"close"];
        cell.deleteButtonOffset = CGPointMake(0, 0);
    }
    
    cell.backgroundColor = [UIColor df_GrayLightColor];
    
    NSDictionary *dic = [self.listData objectAtIndex:index];
    cell.df_titleLabel.text = [dic objectForKey:@"title"];
    cell.df_imageView.image = [UIImage imageNamed:[dic objectForKey:@"img"]];
    
    return cell;
}

- (BOOL)GMGridView:(GMGridView *)gridView canDeleteItemAtIndex:(NSInteger)index
{
    return YES; //index % 2 == 0;
}

- (void)GMGridView:(GMGridView *)gridView didTapOnItemAtIndex:(NSInteger)position
{
    NSLog(@"Did tap at index %d", position);
}

- (void)GMGridViewDidTapOnEmptySpace:(GMGridView *)gridView
{
    NSLog(@"Tap on empty space");
}

- (void)GMGridView:(GMGridView *)gridView processDeleteActionForItemAtIndex:(NSInteger)index
{
    [self br_alertViewConfirmWithTitle:@"确认" content:@"你确定要删除它吗？" operationIndex:^(NSInteger clickedIndex) {
        [self.listData removeObjectAtIndex:index];
        [_gmGridView removeObjectAtIndex:index withAnimation:GMGridViewItemAnimationFade];
    }];
}

- (void)GMGridView:(GMGridView *)gridView changedEdit:(BOOL)edit
{
    if (edit) {
        [self rightBarItem:YES];
    }else
    {
        [self rightBarItem:NO];
    }
}

#pragma mark --

//////////////////////////////////////////////////////////////
#pragma mark GMGridViewSortingDelegate
//////////////////////////////////////////////////////////////

- (void)GMGridView:(GMGridView *)gridView didStartMovingCell:(GMGridViewCell *)cell
{
    [UIView animateWithDuration:0.3
                          delay:0
                        options:UIViewAnimationOptionAllowUserInteraction
                     animations:^{
                         cell.contentView.backgroundColor = [UIColor df_GrayColor];
                         cell.contentView.layer.shadowOpacity = 0.7;
                     }
                     completion:nil
     ];
}

- (void)GMGridView:(GMGridView *)gridView didEndMovingCell:(GMGridViewCell *)cell
{
    [UIView animateWithDuration:0.3
                          delay:0
                        options:UIViewAnimationOptionAllowUserInteraction
                     animations:^{
                         cell.contentView.backgroundColor = [UIColor df_GrayLightColor];
                         cell.contentView.layer.shadowOpacity = 0;
                     }
                     completion:nil
     ];
}

- (BOOL)GMGridView:(GMGridView *)gridView shouldAllowShakingBehaviorWhenMovingCell:(GMGridViewCell *)cell atIndex:(NSInteger)index
{
    return YES;
}

- (void)GMGridView:(GMGridView *)gridView moveItemAtIndex:(NSInteger)oldIndex toIndex:(NSInteger)newIndex
{
    NSObject *object = [self.listData objectAtIndex:oldIndex];
    [self.listData removeObject:object];
    [self.listData insertObject:object atIndex:newIndex];
}

- (void)GMGridView:(GMGridView *)gridView exchangeItemAtIndex:(NSInteger)index1 withItemAtIndex:(NSInteger)index2
{
    [self.listData exchangeObjectAtIndex:index1 withObjectAtIndex:index2];
}
@end
